var time = require("../../../utils/util.js");
var app = getApp();
Page({
  data: {
    message: [],
    code: "",
    page: 1,
    statusBarHeight: app.globalData.statusBarHeight //状态栏高度
  },
  onLoad: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/index',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.code == -2) {
          that.setData({
            code: res.data.code
          })
        } else if (res.data.code == 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            let d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
          }
          that.setData({
            message: res.data.data,
          })
        } else if (res.data.code == -1) {
          that.setData({
            code: res.data.code
          })
        }
      }
    })
  },
  //跳转到注册页面
  zhuce: function(e) {
    wx.navigateTo({
      url: '../login/register/register',
    })
  },
  //行情资讯
  goHQ: function() {
    if (this.data.code == -2) {
      wx.showToast({
        title: '您还没有注册',
        icon: "none",
        duration: 1500,
        mask: true
      })
    } else {
      wx.navigateTo({
        url: 'hangQing/hangQing',
      })
    }
  },
  //委托采购
  goWT: function() {
    if (this.data.code == -2) {
      wx.showToast({
        title: '您还没有注册',
        icon: "none",
        duration: 1500,
        mask: true
      })
    } else {
      wx.navigateTo({
        url: 'weiTuo/weiTuo',
      })
    }
  },
  //金融服务
  goJR: function() {
    if (this.data.code == -2) {
      wx.showToast({
        title: '您还没有注册',
        icon: "none",
        duration: 1500,
        mask: true
      })
    } else {
      wx.navigateTo({
        url: 'jinrong/suiNiYa/suiNiYa',
      })
    }
  },
  //跳转产品详情页面
  goSpecific: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/chanpinjuti/chanpin?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //跳转文章详情页面
  goWenZhang: function(e) {
    wx.navigateTo({
      url: '../jutixiangqing/wenzhangjuti/wenzhang?mid=' + e.currentTarget.dataset.mid + "&uid=" + e.currentTarget.dataset.uid,
    })
  },
  //上拉加载更多
  onReachBottom: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    this.setData({
      page: this.data.page + 1
    })
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/index',
      data: {
        openid: openid,
        page: this.data.page
      },
      method: "post",
      success(res) {
        console.log(res);
        if (res.data.data) {
          for (var i = 0; i < res.data.data.length; i++) {
            //字符串
            if (res.data.data[i].type == 2) {
              if (res.data.data[i].content.length > 24) {
                res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
              }
            }
            //时间
            var d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
            res.data.data[i].createtime = time.timeHandle(d);
            //添加到已有数组message
            that.data.message.push(res.data.data[i])
          }
          that.setData({
            message: that.data.message,
          })
        } else {
          wx.showToast({
            title: '暂无更多信息',
            icon: "none",
            duration: 1500
          })
        }
      }
    });
  },
  //下拉刷新
  onPullDownRefresh: function() {
    var that = this;
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/xinxi/index',
      data: {
        openid: openid,
        page: 1
      },
      method: "post",
      success(res) {
        for (var i = 0; i < res.data.data.length; i++) {
          //字符串
          if (res.data.data[i].type == 2) {
            if (res.data.data[i].content.length > 24) {
              res.data.data[i].content = res.data.data[i].content.substring(0, 24) + '...'
            }
          }
          //时间
          let d = new Date(res.data.data[i].createtime.replace(/-/g, '/')).getTime();
          res.data.data[i].createtime = time.timeHandle(d);
        }
        that.setData({
          message: res.data.data,
          page: 1
        })
        wx.stopPullDownRefresh()
      }
    })
  }
})